package pl.edu.mimuw.mas.agent.behaviour.hunting.grouping;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.edu.mimuw.mas.agent.HuntingAgent;
import pl.edu.mimuw.mas.agent.behaviour.ServerBehaviour;
import pl.edu.mimuw.mas.message.CharismaMessage;
import pl.edu.mimuw.mas.message.PositionMessage;
import pl.edu.mimuw.mas.util.Position;

public class CharismaServerBehaviour extends ServerBehaviour {

	private static final long serialVersionUID = 8669756340882708747L;
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	public CharismaServerBehaviour(HuntingAgent agent) {
		super(agent);
		
		MessageTemplate mt = MessageTemplate.and(
				MessageTemplate.MatchConversationId(HuntingAgent.CHARISMA_CONV),
				MessageTemplate.MatchPerformative(ACLMessage.REQUEST));
		setMessageTemplate(mt);
	}

	@Override
	public boolean doAction(ACLMessage msg, ACLMessage reply) throws UnreadableException, IOException {
		HuntingAgent a = (HuntingAgent) myAgent;
		
		PositionMessage pm = (PositionMessage) msg.getContentObject();
			
		reply.setPerformative(ACLMessage.INFORM);
		if (pm != null) { // pytanie o efektywna charyzme
			CharismaMessage cm = new CharismaMessage(a.getAID(), a.getEffectiveCharisma(new Position(pm.x, pm.y)));
			reply.setContentObject(cm);
		} else { // pytanie o charyzme
			reply.setContentObject(new CharismaMessage(a.getAID(), a.getCharisma()));
		}
	
		return true;
	}

}
